<?php
/**
 * Created by PhpStorm.
 * User: dx
 * Date: 16/5/5
 * Time: 下午3:09
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 
// $cars = DB::table('jd_car_data')->where('id','>','20453')
//     ->select('id','car_model_id')
//     ->get();
// Log::useDailyFiles(storage_path().'/logs/script.log');
//清洗 非标耗材
// $sins_list=SupplyItemNonStandard::where("status",1)->get();
// foreach ($sins_list as $sins) {
//     $fenlei=StatisticalClassification::leftJoIn("statistical_classification as sc","sc.id","=","statistical_classification.parent_id")->
//         leftJoIn("statistical_classification as sc2","sc2.id","=","sc.parent_id")->select("statistical_classification.retrieval_code","statistical_classification.name","sc.retrieval_code as two_rc","sc.name as two_name","sc2.retrieval_code as one_rc","sc2.name as one_name")->where("statistical_classification.id",$sins->st_classifi_id)->first(); 

//     $retrieval_code=$fenlei->one_rc.$fenlei->two_rc.$fenlei->retrieval_code.$fenlei->one_name.$fenlei->two_name.$fenlei->name.$sins->brand_name.$sins->series_name.$sins->model.$sins->code.$sins->specification.$sins->old_code.$sins->car_model;
//     // $pinyin=Pinyin::getShortPinyin($retrieval_code);
//     // $sins->retrieval_code=$retrieval_code.' '.$pinyin;
//     $sins->retrieval_code=$retrieval_code;
//     $sins->save();
//     Log::info($sins->id." 检索码已生成"); 
//     echo $sins->id." 检索码已生成";
// }
// Log::info("非标检索码已生成"); 
// echo "非标检索码已生成";

//标准开始 
// $si_list=SupplyItem::where("status",1)->get();
$si_list=SupplyItem::where("id",">",2073159);



$si_list->chunk(1000, function($silist){                    
    foreach ($silist as $si) {
        Log::info($si->id." 检索码已生成"); 
        // $fenlei=StatisticalClassification::leftJoIn("statistical_classification as sc","sc.id","=","statistical_classification.parent_id")->
        //     leftJoIn("statistical_classification as sc2","sc2.id","=","sc.parent_id")->select("statistical_classification.retrieval_code","sc.retrieval_code as two_rc","sc2.retrieval_code as one_rc")->where("statistical_classification.id",$si->st_classifi_id)->first(); 
        // $fenlei=StatisticalClassification::leftJoIn("statistical_classification as sc","sc.id","=","statistical_classification.parent_id")->
        //     leftJoIn("statistical_classification as sc2","sc2.id","=","sc.parent_id")->select("statistical_classification.retrieval_code","statistical_classification.name","sc.retrieval_code as two_rc","sc.name as two_name","sc2.retrieval_code as one_rc","sc2.name as one_name")->where("statistical_classification.id",$si->st_classifi_id)->first(); 
        // $supply_brand=SupplyBrand::find($si->brand_id);

        if(Cache::has('supply_item_feilie'.$si->supply_classification_id)){//如果有缓存的 标准耗材
            $fenlei=Cache::get('supply_item_feilie'.$si->supply_classification_id);
        }else{
            $fenlei=SupplyClassification::leftJoIn("supply_classification as sc","sc.id","=","supply_classification.parent_id")->
                leftJoIn("supply_classification as sc2","sc2.id","=","sc.parent_id")->select("supply_classification.retrieval_code","supply_classification.name","sc.retrieval_code as two_rc","sc.name as two_name","sc2.retrieval_code as one_rc","sc2.name as one_name")->where("supply_classification.id",$si->supply_classification_id)->first(); 
            if($fenlei){
                Cache::add('supply_item_feilie'.$si->supply_classification_id,$fenlei, 10);
            }
        }
        // $fenlei=SupplyClassification::leftJoIn("supply_classification as sc","sc.id","=","supply_classification.parent_id")->
        //         leftJoIn("supply_classification as sc2","sc2.id","=","sc.parent_id")->select("supply_classification.retrieval_code","supply_classification.name","sc.retrieval_code as two_rc","sc.name as two_name","sc2.retrieval_code as one_rc","sc2.name as one_name")->where("supply_classification.id",$si->supply_classification_id)->first(); 
        $retrieval_code=$fenlei->one_rc.$fenlei->two_rc.$fenlei->retrieval_code.$fenlei->one_name.$fenlei->two_name.$fenlei->name.$si->brand_name.$si->series_name.$si->model.$si->code.$si->old_code.$si->car_model_comments;

        // $retrieval_code=$subject->name.$fenlei->one_rc.$fenlei->two_rc.$fenlei->retrieval_code.$fenlei->one_name.$fenlei->two_name.$fenlei->name.$supply_brand->brand_name.$supply_series->name.$si->model.$si->code.$si->specification.$si->old_code.$si->car_model_comments;
        // $pinyin=Pinyin::getShortPinyin($retrieval_code);

        // $si->retrieval_code=$retrieval_code.' '.$pinyin;
        // $item_info=SupplyItem::getCacheItemInfo($si->id);
        // $si->brand_name=$item_info->brand_name;
        // $si->series_name=$item_info->series_name;
        if(strlen($retrieval_code) > 500){
            $retrieval_code= substr($retrieval_code,0,500);
        }
        $si->retrieval_code=$retrieval_code;
        $si->save();
        Log::info($si->id." 检索码已生成"); 
        echo $si->id." 检索码已生成";
    }
}); 

Log::info("标准检索码已生成"); 
echo "标准检索码已生成";
